package com.sangfor.ssl.l3vpn.service;

import android.text.TextUtils;
import com.sangfor.bugreport.logger.Log;
import com.sangfor.ssl.service.setting.ResourceManager;
import com.sangfor.ssl.service.setting.SystemConfiguration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/* loaded from: classes2.dex */
public class NetworkWhiteListParser {
    private static final String IS_RESOLVED = "1";
    private static final String TAG = "NetworkWhiteListParser";
    private List<Map<String, Object>> ip2Dns;
    private List<AllowType> netWorkWhiteLists;
    private SystemConfiguration sysConf;

    private NetworkWhiteListParser() {
        this.sysConf = SystemConfiguration.getInstance();
        this.netWorkWhiteLists = new ArrayList();
        this.ip2Dns = new ArrayList();
    }

    public static NetworkWhiteListParser getInstance() {
        return InstanceHolder.INSTANCE;
    }

    /* loaded from: classes2.dex */
    private static class InstanceHolder {
        static final NetworkWhiteListParser INSTANCE = new NetworkWhiteListParser();

        private InstanceHolder() {
        }
    }

    public List<AllowType> getNetWorkWhiteList() {
        HashMap hashMap;
        this.netWorkWhiteLists.clear();
        this.ip2Dns.clear();
        HashMap hashMap2 = (HashMap) this.sysConf.get(3, "EMM");
        if (hashMap2 == null || (hashMap = (HashMap) hashMap2.get("NetworkWhiteList")) == null) {
            return null;
        }
        for (Map.Entry entry : hashMap.entrySet()) {
            if (entry.getKey() != null && ((String) entry.getKey()).equals("rule")) {
                ArrayList arrayList = new ArrayList();
                Object value = entry.getValue();
                if (value instanceof Map) {
                    arrayList.add((HashMap) value);
                } else if (!(value instanceof List)) {
                    break;
                } else {
                    arrayList.addAll((ArrayList) value);
                }
                int size = arrayList.size();
                for (int i = 0; i < size; i++) {
                    HashMap hashMap3 = (HashMap) arrayList.get(i);
                    String str = (String) hashMap3.get("host");
                    String str2 = (String) hashMap3.get("is_resolved");
                    if (!TextUtils.isEmpty(str2) && IS_RESOLVED.equals(str2)) {
                        this.netWorkWhiteLists.addAll(handleHost(str));
                    } else {
                        Log.debug(TAG, "Server resolves DNS(whitelist) failed, host is %s", str);
                    }
                }
            }
        }
        return this.netWorkWhiteLists;
    }

    public List<AllowType> handleHost(String str) {
        String htmlDecode = ResourceManager.ResourceHandler.htmlDecode(str);
        ArrayList arrayList = new ArrayList();
        if (htmlDecode.equals("")) {
            return arrayList;
        }
        String trim = htmlDecode.replaceAll("%([^\\d]|$)", "%25$1").trim();
        if (trim.equals("")) {
            return arrayList;
        }
        if (trim.indexOf(":") == -1) {
            if (trim.indexOf("~") == -1) {
                arrayList.add(new AllowType(trim));
            } else {
                String[] split = trim.split("~");
                int length = split.length;
                if (length != 2) {
                    Log.info(TAG, "ip resource is invalid! do not contain char ~");
                    return arrayList;
                }
                for (int i = 0; i < length; i++) {
                    split[i] = split[i].trim();
                }
                arrayList.add(new AllowType(split[0], split[1]));
            }
        } else {
            String[] split2 = trim.split(":");
            if (split2.length != 2) {
                Log.info(TAG, "invalid format for domain ips!");
                return arrayList;
            }
            String str2 = split2[0];
            HashMap hashMap = new HashMap();
            ArrayList arrayList2 = new ArrayList();
            String[] split3 = split2[1].split(",");
            int length2 = split3.length;
            for (int i2 = 0; i2 < length2; i2++) {
                split3[i2] = split3[i2].trim();
                arrayList.add(new AllowType(split3[i2]));
                arrayList2.add(split3[i2]);
            }
            hashMap.put(str2, arrayList2);
            this.ip2Dns.add(hashMap);
        }
        return arrayList;
    }

    public List<Map<String, Object>> getIp2Dns() {
        getNetWorkWhiteList();
        return this.ip2Dns;
    }
}
